package com.maigao.api.handler;

import com.maigao.core.exception.ServerException;
import com.maigao.entity.ResultJson;
import com.maigao.entity.enums.ResultStatus;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.UnexpectedTypeException;
import javax.validation.ValidationException;
import java.util.Set;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2021-08-20 17:58
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    private final Logger log = Logger.getLogger(this.getClass());

    @ResponseBody
    @ExceptionHandler(ValidationException.class)
    public ResultJson handleValidationException(ValidationException exception) {
        if (exception instanceof ConstraintViolationException) {
            ConstraintViolationException exs = (ConstraintViolationException) exception;

            Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
            ConstraintViolation<?> constraintViolation = violations.stream().findFirst().orElse(null);
            if (null != constraintViolation) {
                return new ResultJson(ResultStatus.ERROR.getValue(), constraintViolation.getMessage());
            }
        }
        if (exception instanceof UnexpectedTypeException) {
            UnexpectedTypeException exs = (UnexpectedTypeException) exception;
            return new ResultJson(ResultStatus.ERROR.getValue(), exs.getMessage());
        }
        return new ResultJson(ResultStatus.SUCCESS);
    }

    @ResponseBody
    @ExceptionHandler(DataAccessException.class)
    public ResultJson handleMysqlException(DataAccessException exception) {
        if (exception instanceof DataIntegrityViolationException) {
            DataIntegrityViolationException exs = (DataIntegrityViolationException) exception;
            return new ResultJson(ResultStatus.ERROR.getValue(), exs.getRootCause().getMessage());
        }
        log.error("DB ERROR", exception);
        return new ResultJson(ResultStatus.SUCCESS);
    }

    @ResponseBody
    @ExceptionHandler(BindException.class)
    public ResultJson handleServerException(BindException exception) {
        return new ResultJson(ResultStatus.ERROR.getValue(), exception.getBindingResult().getFieldError().getDefaultMessage());

    }

    @ResponseBody
    @ExceptionHandler(ServerException.class)
    public ResultJson handleException(ServerException exception) {
        if (exception.getMessage().contains("未登录")) {
            return new ResultJson(ResultStatus.TOKEN_FAIL.getValue(), "登录态失效, 请重新登陆");
        }
        return new ResultJson(ResultStatus.ERROR.getValue(), exception.getMessage());
    }
    @ResponseBody
    @ExceptionHandler(JpaSystemException.class)
    public ResultJson handleJpaSystemException(JpaSystemException exception) {
        return new ResultJson(ResultStatus.ERROR.getValue(), "db异常!");
    }

}
